package com.quectel.core.module.schedulingapply.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.schedulingapply.dao.SchedulingApplyRecordAuditLogDao;
import com.quectel.core.module.schedulingapply.dto.SchedulingApplyRecordAuditLogDto;
import com.quectel.core.module.schedulingapply.entity.SchedulingApplyRecordAuditLogEntity;
import com.quectel.core.module.schedulingapply.service.SchedulingApplyRecordAuditLogService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/07/13 19:14
 */
@DubboService
public class SchedulingApplyRecordAuditLogServiceImpl implements SchedulingApplyRecordAuditLogService{

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingApplyRecordAuditLogServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SCHEDULING_APPLY_RECORD_AUDIT_LOG_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SCHEDULING_APPLY_RECORD_AUDIT_LOG_CACHE_BY_ID:%d", id);
    private static final String SCHEDULING_APPLY_RECORD_AUDIT_LOG_CACHE_BY_RECORD_ID = RedisCacheConstants.PROJECT_NAME + "SCHEDULING_APPLY_RECORD_AUDIT_LOG_CACHE_BY_RECORD_ID:";

    /**
     * 清除缓存方法
     */
    private static final Consumer<SchedulingApplyRecordAuditLogDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(SCHEDULING_APPLY_RECORD_AUDIT_LOG_CACHE_BY_ID_FUNC.apply(dto.getId()));
        CacheKit.invalidRedisCache(SCHEDULING_APPLY_RECORD_AUDIT_LOG_CACHE_BY_RECORD_ID + dto.getApplyRecordId());

    };


    @Autowired
    private SchedulingApplyRecordAuditLogDao schedulingApplyRecordAuditLogDao;
    @Autowired
    private DeptPersonService deptPersonService;



    @Override
    public List<SchedulingApplyRecordAuditLogDto> selectCacheByRecordId(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectByRecordId(id),
                SCHEDULING_APPLY_RECORD_AUDIT_LOG_CACHE_BY_RECORD_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public List<SchedulingApplyRecordAuditLogDto> selectByRecordId(Long id) {
        List<SchedulingApplyRecordAuditLogEntity> visitorApplyRecordAuditLogEntities = schedulingApplyRecordAuditLogDao.selectList(new LambdaQueryWrapper<SchedulingApplyRecordAuditLogEntity>()
                .eq(SchedulingApplyRecordAuditLogEntity::getApplyRecordId, id));
        List<SchedulingApplyRecordAuditLogDto> result = CopyUtils.copyList(visitorApplyRecordAuditLogEntities, SchedulingApplyRecordAuditLogDto.class);
        result.forEach(v -> paddingField(v));
        return result;
    }



    @Override
    public SchedulingApplyRecordAuditLogDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SCHEDULING_APPLY_RECORD_AUDIT_LOG_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SchedulingApplyRecordAuditLogDto selectById(Long id){
            SchedulingApplyRecordAuditLogEntity entity = schedulingApplyRecordAuditLogDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,SchedulingApplyRecordAuditLogDto.class));
    }

    @Override
    public List<SchedulingApplyRecordAuditLogDto> queryList(Map<String,Object> params){
        List<SchedulingApplyRecordAuditLogEntity> list = schedulingApplyRecordAuditLogDao.queryList(params);
        List<SchedulingApplyRecordAuditLogDto> result =  CopyUtils.copyList(list,SchedulingApplyRecordAuditLogDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingApplyRecordAuditLogDto> queryListWithoutPadding(Map<String,Object> params){
        List<SchedulingApplyRecordAuditLogEntity> list = schedulingApplyRecordAuditLogDao.queryList(params);
        return CopyUtils.copyList(list,SchedulingApplyRecordAuditLogDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private SchedulingApplyRecordAuditLogDto paddingField(SchedulingApplyRecordAuditLogDto dto){
        if (dto != null){
            DeptPersonDto deptPersonDto = deptPersonService.selectCacheById(dto.getAuditDeptPersonId());
            if(deptPersonDto!=null){
                dto.setAuditDeptPersonName(deptPersonDto.getName());
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return schedulingApplyRecordAuditLogDao.queryTotal(params);
    }

    @Override
    public Long save(SchedulingApplyRecordAuditLogDto dto){
        SchedulingApplyRecordAuditLogEntity entity = CopyUtils.copyObj(dto,SchedulingApplyRecordAuditLogEntity.class);
        entity.setId(Snowflake.nextId());
        schedulingApplyRecordAuditLogDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SchedulingApplyRecordAuditLogDto dto){
        SchedulingApplyRecordAuditLogEntity entity = CopyUtils.copyObj(dto,SchedulingApplyRecordAuditLogEntity.class);

        schedulingApplyRecordAuditLogDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(SchedulingApplyRecordAuditLogDto dto) {
       SchedulingApplyRecordAuditLogEntity entity = CopyUtils.copyObj(dto,SchedulingApplyRecordAuditLogEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, schedulingApplyRecordAuditLogDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        schedulingApplyRecordAuditLogDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
